# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen
from com.zzh.utils.pagingUtils import MyPaging

class HistoryDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertHis(self, info):
        """
        添加 hisroty 信息
        :param info:
        :return:
        """
        userId = info['userId']
        userIp = info['userIp']
        hisKeywords = info['hisKeywords']
        hisType = info['hisType']
        hisSearchCount = info['hisSearchCount']

        sql = "insert into zzh_history (user_id, user_ip, history_keywords, history_type," \
              "history_searchcount) VALUES " \
              "(%s, %s, %s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, userIp, hisKeywords, hisType, hisSearchCount])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findHisById(self, hisId):
        """
        根据ID查询 history 信息
        :param hisId:
        :return:
        """
        sql = "select * from zzh_history WHERE history_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [hisId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.history_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findHisByCondition(self, **kwargs):
        """
        history 表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.historyFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_history WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_history WHERE " + whereSentence
        rs = cur.execute(sql)

        jsons = None
        if rs:
            list0 = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, list0)
            jsons = Table2Json.history_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons, rs

    def deleteHis(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_history WHERE history_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateHis(self, info):
        """
        更新 history 表
        :param info:
        :return:
        """
        hisId = info['hisId']
        userId = info['userId']
        userIp = info['userIp']
        hisKeywords = info['hisKeywords']
        hisType = info['hisType']
        hisSearchCount = info['hisSearchCount']

        sql = "update zzh_history set user_id=%s,user_ip=%s,history_keywords=%s, history_type=%s,history_searchcount=%s" \
              " WHERE history_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, userIp, hisKeywords, hisSearchCount, hisType, hisId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectHis(self,hisId):
        """
        根据ID查询，返回元组
        :param hisId:
        :return:
        """
        sql = "select * from zzh_history WHERE history_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [hisId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def queryHistoryLimit5(self, userId):
        """
        根据user ID查询 history 信息, 返回top 5
        :param hisId:
        :return:
        """
        sql = "select * from zzh_history WHERE user_id=%s ORDER BY history_searchcount limit 5 "

        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.history_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def clearHistorys(self, userId):
        sql = "delete from zzh_history WHERE user_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId])
        self.conn.commit()

        cur.close()
        self.conn.close()
        return rs

    def t1(self,userId,hisType,keyword):
        sql = "SELECT history_id,history_searchcount FROM zzh_history WHERE user_id=%s AND history_type=%s AND history_keywords=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql,[userId,hisType,keyword])

        if rs:
            list0 = cur.fetchone()
            cur.close()
            self.conn.close()
            return list0
        else:
            cur.close()
            self.conn.close()
            return 0
    def updateHiCount(self,hisId,hisCount):
        hisCount2 = hisCount+1
        sql = "update zzh_history set history_searchcount=%s WHERE history_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [hisCount2 ,hisId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs